package com.springapp.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.springapp.jsonmodel.TableJson;
import com.springapp.model.Desk;
import com.springapp.model.Order;
import com.springapp.model.OrderStatus;
import com.springapp.model.Shape;
import com.springapp.model.Status;
import com.springapp.service.OrderService;
import com.springapp.service.TableService;
import com.springapp.service.impl.TableServiceImpl;

import java.util.ArrayList;
import java.util.List;
import com.springapp.util.ControllerUtil;

/**
 * Created with IntelliJ IDEA. User: kataeva.nat
 */
@Controller
@RequestMapping("/")
public class TableController {

    @Autowired
    private TableService tableService;
    @Autowired
    private OrderService orderService;
    private final Logger log = LoggerFactory.getLogger(TableController.class);

    /**
     *
     * @author vkrivtc
     */
    @RequestMapping(value = "/table/{id}", method = RequestMethod.GET)
    public @ResponseBody
    String getById(@PathVariable("id") Integer id) {
        Desk desk = tableService.getById(id);
        if (desk == null) {
            return ControllerUtil.printError();
        }
        log.info("one table -" + desk);
        GsonBuilder gsonBuilder = new GsonBuilder();
        Gson gson = gsonBuilder.create();
        TableJson tableJson = TableJson.toModel(desk);
        String json = gson.toJson(tableJson);
        return ControllerUtil.printSuccess(json);
    }

    /**
     *
     * @author vkrivtc
     */
    @RequestMapping(value = "/table", method = RequestMethod.GET)
    public @ResponseBody
    String getAll() {
        List<Desk> desks = tableService.getAll();
        log.info("tables arr -" + desks);
        List<TableJson> json = new ArrayList<TableJson>();
        for (int i = 0; i < desks.size(); i++) {
            json.add(TableJson.toModel(desks.get(i)));
        }
        log.info("tables json arr -" + json);
        GsonBuilder builder = new GsonBuilder();
        Gson gson = builder.create();
        String jsonTables = gson.toJson(json);
        return ControllerUtil.printSuccess(jsonTables);
    }

    @RequestMapping(value = "/table", method = RequestMethod.POST, consumes = "application/json")
    public @ResponseBody
    String add(@RequestBody String json) {
        Gson gson = new Gson();
        TableJson tableJson = gson.fromJson(json, TableJson.class);
        Desk desk = new Desk();
        desk.setPositionX(tableJson.getX());
        desk.setPositionY(tableJson.getY());
        desk.setOrder(null);
        desk.setStatus(Status.FREE);
        desk.setShape(tableJson.getShape());
        tableService.create(desk);
        return ControllerUtil.printSuccess(json);
    }

    @RequestMapping(value = "/table/{id}", method = RequestMethod.PUT, consumes = "application/json")
    public @ResponseBody
    String update(@RequestBody String json, @PathVariable int id) {
        Gson gson = new Gson();
        TableJson tableJson = gson.fromJson(json, TableJson.class);
        Desk desk = tableService.getById(id);
        if (desk == null) {
            return ControllerUtil.printError();
        }
        desk.setPositionX(tableJson.getX());
        desk.setPositionY(tableJson.getY());
        Status status = tableJson.getStatus();
        desk.setStatus(status);
        Shape shape = tableJson.getShape();
        desk.setShape(shape);
        tableService.update(desk);
        return ControllerUtil.printSuccess(json);
    }

    /**
     *
     * @author vkrivtc
     */
    @RequestMapping(value = "/table/{id}", method = RequestMethod.DELETE, consumes = "application/json")
    public @ResponseBody
    String delete(@PathVariable int id) {
        Desk desk = tableService.getById(id);
        if (desk == null) {
            return ControllerUtil.printError();
        }
        TableJson tableJson = TableJson.toModel(desk);
        Gson gson = new GsonBuilder().create();
        String json = gson.toJson(tableJson);
        tableService.delete(id);
        return ControllerUtil.printSuccess(json);

    }
}
